define(function (require) {
    var ko = require("knockout");
    var komapping = require("komapping");

    var dateFtt = function (fmt, date) { //author: meizz
        var o = {
            "M+": date.getMonth() + 1, //月份
            "d+": date.getDate(), //日
            "h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小时
            "H+": date.getHours(), //小时
            "m+": date.getMinutes(), //分
            "s+": date.getSeconds(), //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    };

    var setParamFromUrl = function (param, queryParam) {
        //重置部分参数防止缓存
        param.actionId("");
        param.searchDate("");

        if (!((typeof queryParam === "object") && queryParam)) {
            queryParam = {};
        }
        if ((typeof queryParam.actionId === "string") && queryParam.actionId) {
            param.actionId(queryParam.actionId);
        } else {
            if ((typeof queryParam.searchDate === "string") && queryParam.searchDate) {
                param.searchDate(queryParam.searchDate);
            } else {
                param.searchDate(dateFtt("yyyy-MM-dd HH:mm:ss", new Date()));
            }
        }
        if ((typeof queryParam.codeArrayInclude === "string") && queryParam.codeArrayInclude) {
            param.codeArrayInclude(queryParam.codeArrayInclude);
        } else {
            param.codeArrayInclude("");
        }
        if ((typeof queryParam.codeArrayExclude === "string") && queryParam.codeArrayExclude) {
            param.codeArrayExclude(queryParam.codeArrayExclude);
        } else {
            param.codeArrayExclude("");
        }
        if ((typeof queryParam.hostInclude === "string") && queryParam.hostInclude) {
            param.hostInclude(queryParam.hostInclude);
        } else {
            param.hostInclude("");
        }
        if ((typeof queryParam.hostExclude === "string") && queryParam.hostExclude) {
            param.hostExclude(queryParam.hostExclude);
        } else {
            param.hostExclude("");
        }
        if ((typeof queryParam.refererInclude === "string") && queryParam.refererInclude) {
            param.refererInclude(queryParam.refererInclude);
        } else {
            param.refererInclude("");
        }
        if ((typeof queryParam.refererExclude === "string") && queryParam.refererExclude) {
            param.refererExclude(queryParam.refererExclude);
        } else {
            param.refererExclude("");
        }
        if ((typeof queryParam.protocolInclude === "string") && queryParam.protocolInclude) {
            param.protocolInclude(queryParam.protocolInclude);
        } else {
            param.protocolInclude("");
        }
        if ((typeof queryParam.protocolExclude === "string") && queryParam.protocolExclude) {
            param.protocolExclude(queryParam.protocolExclude);
        } else {
            param.protocolExclude("");
        }
        if ((typeof queryParam.urlInclude === "string") && queryParam.urlInclude) {
            param.urlInclude(queryParam.urlInclude);
        } else {
            param.urlInclude("");
        }
        if ((typeof queryParam.urlExclude === "string") && queryParam.urlExclude) {
            param.urlExclude(queryParam.urlExclude);
        } else {
            param.urlExclude("");
        }
        if ((typeof queryParam.typeInclude === "string") && queryParam.typeInclude) {
            param.typeInclude(queryParam.typeInclude);
        } else {
            param.typeInclude("");
        }
        if ((typeof queryParam.typeExclude === "string") && queryParam.typeExclude) {
            param.typeExclude(queryParam.typeExclude);
        } else {
            param.typeExclude("");
        }
        if ((typeof queryParam.minSize === "string") && queryParam.minSize) {
            param.minSize(queryParam.minSize);
        } else {
            param.minSize("");
        }
    };

    var searchUrlGen = function (param, prefix) {
        return prefix + "?" +
            "searchDate=" + encodeURIComponent(param.searchDate()) +
            "&actionId=" + encodeURIComponent(param.actionId()) +
            "&codeArrayInclude=" + encodeURIComponent(param.codeArrayInclude()) +
            "&codeArrayExclude=" + encodeURIComponent(param.codeArrayExclude()) +
            "&hostInclude=" + encodeURIComponent(param.hostInclude()) +
            "&hostExclude=" + encodeURIComponent(param.hostExclude()) +
            "&refererInclude=" + encodeURIComponent(param.refererInclude()) +
            "&refererExclude=" + encodeURIComponent(param.refererExclude()) +
            "&protocolInclude=" + encodeURIComponent(param.protocolInclude()) +
            "&protocolExclude=" + encodeURIComponent(param.protocolExclude()) +
            "&urlInclude=" + encodeURIComponent(param.urlInclude()) +
            "&urlExclude=" + encodeURIComponent(param.urlExclude()) +
            "&typeInclude=" + encodeURIComponent(param.typeInclude()) +
            "&typeExclude=" + encodeURIComponent(param.typeExclude()) +
            "&minSize=" + encodeURIComponent(param.minSize());
    };

    return {
        setParamFromUrl: setParamFromUrl,
        searchUrlGen: searchUrlGen
    };

});